package com.lxx.onlineview.service;

import com.lxx.onlineview.entity.po.Student;
import com.lxx.onlineview.entity.po.Teacher;
import com.lxx.onlineview.entity.po.User;
import com.lxx.onlineview.mapper.UserMapper;
import com.lxx.onlineview.results.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.lxx.onlineview.results.CodeMsg.*;

@Service
public class UserService {
    @Autowired
    UserMapper userMapper;

    public Result<User> getByUserName(String userName) {
        User user = userMapper.selectByPrimaryKey(userName);
        if(user==null) {
            return Result.error(NOT_EXIST);
        } else {
            return Result.success(user);
        }
    }

    public Result<List<User>>  getByType(String type){
        List<User> user = userMapper.selectAll(type);
        if(user==null||user.isEmpty()){
            return Result.error(NOT_EXIST);
        }else{
            return Result.success(user);
        }
    }

    public Result<String> login(String userName, String password) {
        User user = userMapper.selectByUserAndPwd(userName,password);
        if (user == null) {
            return Result.error(NOT_EXIST);
        }else{
            switch (user.getType()){
                case 0:{
                    return Result.success("0");
                }
                case 1:{
                    return Result.success("1");
                }
                case 2:{
                    return Result.success("2");
                }
                default:{
                    return Result.error(NOT_EXIST);
                }
            }
        }
    }

    public Result<String> setUser(User user) {
        int result = userMapper.insert(user);
        if(result==1){
            return Result.success("注册成功");
        }else {
            return Result.error(ADD_FAIL);
        }
    }

    public Result<User> updateUser(User user){
        int result = userMapper.updateByPrimaryKey(user);
        if (result==1){
            return Result.success(user);
        }else{
            return Result.error(UPDATE_FAIL);
        }
    }


}
